<template>
  <div>
    <div id="shopcar" ref="shopCar">
      <div class="header1">
        <div class="header">
          <img src="./../../common/img/jdlogo.png" class="header_logo" />
          <span>{{ $t("i18n.car") }}</span>

          <div class="search">
            <el-input
              :placeholder =change1
              
              class="input"
              v-model="input"
              clearable
            >
            </el-input>
            <div
              class="el-icon-search search_Button"
              style="display: inline-block"
              @click="search"
            >
              {{$t("i18n.search")}}
            </div>
          </div>

          <div class="right">
            <el-button type="danger" plain @click="change">{{
              $t("i18n.change")
            }}</el-button>
          </div>
        </div>
      </div>
      <div class="dd">
        <div id="content">
          <div class="ddd"></div>
          <div class="cont_title">
            <span>{{ $t("i18n.all") }}：{{ this.allNumber }}</span>
          </div>
          <div class="cont_op">
            <div class="con_selet">
              <input
                type="checkbox"
                class="con_selectAll"
                :checked="isSelectedAll"
                @click.stop="selectedAll(isSelectedAll)"
              />
              <label for="con_selectAll">{{ $t("i18n.allSelect") }}</label>
            </div>
            <div>{{ $t("i18n.Detail") }}</div>
            <div>{{ $t("i18n.singlePrice") }}</div>
            <div>{{ $t("i18n.number") }}</div>
            <div>{{ $t("i18n.price") }}</div>
            <div>{{ $t("i18n.play") }}</div>
          </div>

          <div class="pro_box" v-if="cartgoods.length">
            <div
              class="goods"
              v-for="(goods, index) in !searchArr.length
                ? cartgoods
                : searchArr"
              :key="index"
            >
              <ul class="item_content">
                <li class="td td-chk">
                  <div>
                    <input
                      type="checkbox"
                      class="checkBox"
                      :checked="goods.checked"
                      @click="singleSelected(goods)"
                    />
                  </div>
                </li>
                <li class="td td-item">
                  <div class="td-item-pic">
                    <img :src="goods.thumb_url" />
                  </div>
                  <div class="td-item-info">{{ goods.goods_name }}</div>
                </li>
                <li class="td td-price">
                  <strong>{{ (goods.price / 100) | moneyFormat }}</strong>
                </li>
                <li class="td td-amount">
                  <div class="item-amout">
                    <el-input-number
                      v-model="goods.buy_count"
                      :min="1"
                      :max="goods.counts"
                      @change="updateGoodsCount(goods, goods.buy_count)"
                    ></el-input-number>
                  </div>
                </li>
                <li class="td td-sum">
                  <strong>{{
                    ((goods.buy_count * goods.price) / 100) | moneyFormat
                  }}</strong>
                </li>
                <li class="td td-op">
                  <a @click="clickTrash(goods)"
                    ><el-button size="mini" type="danger" round>{{
                      $t("i18n.delete")
                    }}</el-button></a
                  >
                </li>
              </ul>
            </div>
          </div>

          <div class="pro_box center" v-else>
            <div class="empty">{{ $t("i18n.notSelect") }}</div>
          </div>
        </div>
        <div class="footer" :class="{ bottom: !isBottom }">
          <div class="foot_select">
            <input
              type="checkbox"
              class="foot_selectAll"
              name="foot_selectAll"
              :checked="isSelectedAll"
              @click.stop="selectedAll(isSelectedAll)"
            />
            <label for="foot_selectAll">{{ $t("i18n.allSelect") }}</label>
          </div>
          <div class="foot_op">
            <a class="foot_remove" @click.prevent="emptyCart">{{
              $t("i18n.clearCar")
            }}</a>
          </div>
          <div class="foot_total">
            <div class="amout-sum">
              <span class="txt">{{ $t("i18n.alreadySelect") }}</span>
              <strong id="selected_amout">{{ this.allNumber }}</strong>
              <span class="txt">{{ $t("i18n.jian") }}</span>
            </div>
            <div class="price-sum">
              <span class="txt">{{ $t("i18n.allPrice") }}：</span>
              <strong class="selected_price">{{
                totalPrice | moneyFormat(totalPrice)
              }}</strong>
            </div>
            <div class="btn-area">
              <el-button type="danger" round>{{ $t("i18n.pay") }}</el-button>
              <el-row></el-row>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="allMap" v-move @mouseover="show" @mouseleave="none">
      <img
        ref="allMap"
        src="../../common/img/dog.gif"
        alt=""
        style="width: 100%; height: 100%"
      />
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { mapActions } from "vuex";
import { MessageBox } from "element-ui";

export default {
  data() {
    return {
      isBottom: false,
      input: "",
      shopsNum: [],
      isSelectedAll: false,
      totalPrice: 0, // 商品总价
      currentDelGoods: {}, // 当前删除的商品
      totalAmount: 0,
      allNumber: 0,
      searchArr: [],

    };
  },
  computed: {
    change1(){
        return  this.$t('i18n.inContent')
      },
    ...mapState(["userInfo", "cartgoods"])

      
  },
  mounted() {
    let user_id = this.userInfo.id;
    // 请求商品数据
    this.$store.dispatch("reqCartsGoods", { user_id });
    window.onscroll = this.throttle(this.onScroll);
  },
  methods: {
    search() {
      if (!this.input.trim()) {
        this.searchArr = [];
      } else {
        this.searchArr = this.cartgoods.filter((item) => {
          let a = item.goods_name;
          for (let i = 0; i < a.length; i++) {
            if (this.input.indexOf(a[i]) !== -1) return true;
          }
        });
        if (!this.searchArr.length) {
          this.$message({
            message: "抱歉没有找到",
            type: "error",
          });
          return
        }

        this.$message({
          message: "加载成功",
          type: "success",
        });
      }
    },
    // 鼠标经过
    show() {
      this.$refs.allMap.src = require("../../common/img/dog_hover.gif");
    },
    none() {
      this.$refs.allMap.src = require("../../common/img/dog.gif");
    },
    // 1.更新单个商品数量
    updateGoodsCount(goods, count) {
      let user_id = this.userInfo.id;
      this.$store.dispatch("updateGoodsCount", { goods, count, user_id });
      this.getAllGoodsPrice();
      this.getNumber();
    },
    //改变 语言
    change() {
      this.$i18n.locale = this.$i18n.locale == "zh" ? "han" : "zh";
      if (this.$i18n.locale == "zh") {
        this.$notify({
          title: "成功",
          message: "切换成功",
          type: "success",
          position: "bottom-right",
        });
      } else {
        this.$notify({
          title: "성공",
          message: "성공 스위치",
          type: "success",
          position: "bottom-right",
        });
      }
    },
    // 2.是否选中所有商品
    selectedAll(isSelectedAll) {
      this.isSelectedAll = !isSelectedAll;
      this.$store.dispatch("selectedAll", { isSelectedAll });
      this.getAllGoodsPrice();
      this.getNumber();
    },
    // 3.计算商品总价
    getAllGoodsPrice() {
      let totalPrice = 0;
      this.cartgoods.forEach((goods, index) => {
        if (goods.checked) {
          totalPrice += (goods.price / 100) * goods.buy_count;
        }
      });
      this.totalPrice = totalPrice;
    },
    // 4.单个商品的选中与否
    singleSelected(goods) {
      this.$store.dispatch("singleSelected", { goods });
      this.hasSelectedAll();
      this.getAllGoodsPrice();
      this.getNumber();
    },
    // 5.判断是否全选
    hasSelectedAll() {
      let flag = true;
      let totalNum = 0;
      this.cartgoods.forEach((goods, index) => {
        if (!goods.checked) {
          flag = false;
        } else {
          totalNum += 1;
        }
      });
      this.totalAmount = totalNum;
      this.isSelectedAll = flag;
      this.getNumber();
    },
    // 6.删除单个商品
    clickTrash(goods) {
      this.$confirm("您确定删除该商品吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let user_id = this.userInfo.id;
          this.currentDelGoods = goods;
          this.$store.dispatch("delSingleGoods", { goods, user_id });
          this.getAllGoodsPrice();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.getNumber();
    },
    // 7.删除所有商品
    emptyCart() {
      this.$confirm("您确定删除所有商品吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let user_id = this.userInfo.id;
          this.$store.dispatch("delAllGoods", { user_id });
          this.getAllGoodsPrice();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      this.getNumber();
    },
    // 选中的全部商品数量
    getNumber() {
      let num = 0;
      this.cartgoods.forEach((item) => {
        if (item.checked) num += item.buy_count;
      });
      console.log(num);
      this.allNumber = num;
    },
    // 是否到达底部
    onScroll() {
      let scrollTop = document.documentElement.scrollTop;
      let scrollHeight = document.documentElement.scrollHeight - 40;

      let clientHeight = document.documentElement.clientHeight;

      if (scrollHeight <= scrollTop + clientHeight) {
        this.isBottom = true;
      } else {
        this.isBottom = false;
      }
    },
    // 节流
    throttle(fn, delay = 300) {
      let time = null;
      return function () {
        if (!time) {
          time = setTimeout(() => {
            fn();
            time = null;
          }, delay);
        }
      };
    },
  },
};
</script>

<style scoped>
.header_logo{
  width: 100px;
}
.search {
  display: inline-block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.allMap {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 80%;
  z-index: 10;
  border-radius: 10px;
  right: 3%;
}
.header1 {
  width: 100%;
  height: 90px;
  background-color: #fff;
}
.ddd {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  background-color: #fff;
  margin-bottom: 20px;
}
.dd {
  overflow: auto;
  width: 100%;
  height: 100%;
  z-index: 333;
  margin: 0 auto;
}
#shopcar {
  width: 100%;
  background-color: #e8e3e1;
  overflow: hidden;
  position: relative;
}
.center {
  width: 100%;
  height: 300px;
  line-height: 300px;
  text-align: center;
  font-size: 20px;
  border-bottom: 1px solid #e8e3e1;
}

.header {
  width: 80%;
  margin: 0 auto;
  height: 90px;
  background-color: #fff;
  position: relative;
}
.header > img {
  width: 142px;
  height: 63px;
  margin-top: 5px;
  cursor: pointer;
}
.header > span {
  display: inline-block;
  font-size: 25px;
  height: 35px;
  font-weight: 700;
  position: absolute;
  top: 30%;
  margin-left: 20px;
  cursor: pointer;
}
#content {
  width: 80%;
  margin: 0 auto;
  background-color: white;
  border-top-left-radius: 30px;
  border-top-right-radius: 30px;
}
.right {
  display: inline-block;
  float: right;
  line-height: 90px;
}
#content > .cont_title {
  margin: 10px auto;
  width: 100%;
  height: 70px;
  line-height: 70px;
  border-bottom: 2px solid #e8e3e1;
}
.cont_title > span {
  display: inline-block;
  font-weight: 700;
  font-size: 17px;
  height: 100%;
  width: 300px;
  text-align: left;
  margin-left: 60px;
  line-height: 25px;
  cursor: pointer;
  color: #f40;
}

>>> .el-input__inner {
  padding: 0 26px;
}
>>> .el-input-number__decrease,
>>> .el-input-number__increase {
  width: 20px;
}
/deep/.el-input-number {
  width: 70px;
}
.input {
  display: inline-block;
  width: 300px;
  vertical-align: bottom;
}
.search_Button {
  width: 80px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 10px;
  font-size: 16px;
  color: white;
  background-color: red;
}
.search_Button:hover {
  cursor: pointer;
}
#content > .cont_op {
  width: 96%;

  margin: 0 auto;
  padding: 0 50px;
  height: 40px;
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.cont_op > div {
  line-height: 40px;
}
.cont_op > div:nth-child(2) {
  width: 120px;
}
#content > .pro_box {
  margin: 0 auto;
  width: 100%;
}
.pro_box > .goods {
  width: 100%;
  padding: 0 10px;
  height: 130px;
  margin: 20px 0;
  box-sizing: border-box;
}
.goods > .item_content {
  background: #f5f5f5;
  border: 1px solid #cccccc;
  border-radius: 20px;
  padding: 15px;
  margin-right: -10px;
  list-style: none;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.item_content > .td-item {
  width: 180px;
  position: relative;
}
.td-item > div {
  display: inline-block;
}
.td-item > .td-item-pic > img {
  width: 80px;
  height: 80px;
}
.td-item > .td-item-info {
  position: absolute;
  width: 80px;
  line-height: 15px;
  font-size: 12px;
  top: 0;
  margin-left: 20px;
}
.item_content > .td-sum {
  color: #f40;
  text-align: center;
}
.td-amount > .item-amout {
  height: 25px;
}
.item-amout a {
  display: inline-block;
  height: 23px;
  width: 17px;
  border: 1px solid #e5e5e5;
  background: #f0f0f0;
  text-align: center;
  line-height: 23px;
  color: #444;
  cursor: pointer;
}
.item-amout a:hover {
  color: #f50;
  border-color: #f60;
}
.item-amout > .text_amount {
  width: 40px;
  height: 20px;
  text-align: center;
  display: inline-block;
}
.td-op a {
  color: black;
}
.td-op a:hover {
  cursor: pointer;
  color: #f40;
}
.footer {
  width: 80%;
  height: 80px;
  padding: 0 10px;
  line-height: 80px;
  position: sticky;
  bottom: 20px;
  left: 10%;
  background: white;
  border-top: 1px solid #e8e3e1;
  z-index: 1;
  border-bottom-left-radius: 30px;
  border-bottom-right-radius: 30px;
}
.bottom {
  position: fixed;
  bottom: 0px;
  border-radius: 0;
}
.footer div {
  display: inline-block;
  margin-left: 30px;
}
.footer > .foot_select {
  height: 50px;
  line-height: 50px;
  padding-left: 5px;
  cursor: pointer;
  font-size: 17px;
}
.foot_select input,
.foot_select label {
  cursor: pointer;
}
.foot_op {
  line-height: 50px;
  height: 50px;
}
.foot_op > .foot_remove {
  margin-left: 25px;
  font-size: 14px;
  cursor: pointer;
}
.foot_remove:hover {
  color: #f40;
}
.foot_total {
  float: right;
}
.foot_total > .amout-sum {
  cursor: pointer;
  height: 50px;
  color: #3c3c3c;
}
.foot_total .txt {
  line-height: 50px;
  font-size: 14px;
}
.amout-sum > #selected_amout {
  padding: 0 5px;
  color: #f40;
  font-weight: 700;
  font-size: 18px;
}
.price-sum {
  margin-left: 50px;
}
.price-sum > .selected_price {
  color: #f40;
  font-weight: 700;
  font-size: 20px;
  line-height: 50px;
}
.btn-area > .btn-sumbit {
  display: inline-block;
  background: red;
  color: #fff;
  border-left: 1px solid #e7e7e7;
  width: 119px;
  height: 50px;
  cursor: not-allowed;
  text-align: center;
  line-height: 50px;
  font-size: 20px;
}
.btn-area > .btn-sumbit.btn-allow {
  background: #f22d00;
  cursor: pointer;
}
</style>
